package com.mhys.hms.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

/**
 * 跨域请求
 *
 */
public class CROSFilter implements Filter {

	//allowed domain
	private String domain= "*";
	
	@Override
	public void destroy() {
		domain = null;	
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletResponse resp = (HttpServletResponse) response;
		resp.setHeader("Access-Control-Allow-Origin", this.domain);
		
		chain.doFilter(request, resp);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String v = filterConfig.getInitParameter("domain");
		if(!StringUtils.isEmpty(v)) {
			this.domain =v.trim();
		}	
	}


}
